<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class User_guanzhu extends CI_Model {

	const DB_GUANZHU = 'user_guanzhu';
	const DB_USER = 'user';	

	function __construct()
	{
		parent::__construct();
	
	}
	//查询UID所关注的人
	function get_user_by_uid($user_id, $offset = 0, $limit = 6)
	{
		$data = array();
		$this->db->select("".self::DB_USER.".id, ".self::DB_USER.".username");
		$this->db->join(self::DB_GUANZHU, "".self::DB_GUANZHU.".guanzhu_id = ".self::DB_USER.".id");
		$this->db->where("uid", $user_id);
		$this->db->limit(intval($limit));
		$this->db->offset(intval($offset));
		$query = $this->db->get(self::DB_USER);
		foreach ($query->result_array() as $user)
		{
			$user['headicon'] = $this->user_login->get_avatar_dir($user['id']);
			$data[] = $user;
		}
		
		return $data;
	}

	//关注
	function get_user_guanzhu($guanzhu_id, $user_id)
	{
		$array = array(
			'guanzhu_id' 	=> intval($guanzhu_id),
			'uid' 			=> intval($user_id)
		);

		return $this->db->get_where(self::DB_GUANZHU, $array)->num_rows();
	}
	//添加关注
	function add_user_guanzhu($guanzhu_id, $user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id),
			'guanzhu_id' => intval($guanzhu_id)
		);

		return $this->db->insert(self::DB_GUANZHU, $array);
	}
	//删除关注
	function del_user_guanzhu($guanzhu_id, $user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id),
			'guanzhu_id' => intval($guanzhu_id)
		);
		return $this->db->delete(self::DB_GUANZHU, $array);
	}

	function del_user($user_id)
	{
		$array = array(
			'uid' 		=> intval($user_id)
		);
		return $this->db->delete(self::DB_GUANZHU, $array);
	}
}

/* End of file user_guanzhu.php */
/* Location: ./application/models/user_guanzhu.php */